功能:把兩個或多個可迭代的物件聚合起來
1.物件
usernames = ["Bob", "Steven", "Sam"]
passwords = ("123", "321", "555")
user =zip(usernames, passwords)
for i in user:
print(i)
('Bob', '123')
('Steven', '321')
('Sam', '555')
2.列表
usernames = ["Bob", "Steven", "Sam"]
passwords = ("123", "321", "555")
user =zip(usernames, passwords)
print( list(user))
[('Bob', '123'),('Steven', '321'),('Sam', '555')]
3.字典
usernames = ["Bob", "Steven", "Sam"]
passwords = ("123", "321", "555")
user =zip(usernames, passwords)
user_dict = dict(user)
print(user_dict)
{('Bob', '123'),('Steven', '321'),('Sam', '555')}
輸入兩個以上元素時不能直接轉型成列表或是字典
開始前準備兩個檔案:模組1、模組2
模組1:
import moudle2
moudle2.hello()
print("模組1 __name__:" + __name__)
if __name__ == '__main__':
print('模組1 __name__ == __main__')
模組2 __name__: moudle2
hello
模組1 __name__: moudle1
模組2 __name__: __main__
模組2 __name__: == __main__
模組2:
print("模組2 __name__:" + __name__)
if __name__ == '__main__':
print('模組2 __name__ == __main__')
def hello():
print("hello")
模組2 __name__: __main__
模組2 __name__: == __main__